package tech.daoxuan.web.controller;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import tech.daoxuan.web.exception.UserNotExitException;

import java.util.HashMap;
import java.util.Map;

/**
 * 处理其他controller抛出的异常
 */
@ControllerAdvice
public class ExceptionHandlerController {


    /**
     * {@link ExceptionHandler} 当任何方法抛出{@link UserNotExitException} 时，都会用此方法处理
     * {@link ResponseBody}  返回 json格式
     * {@link ResponseStatus} 确定异常返回的状态码
     * @return
     */
    @ExceptionHandler(UserNotExitException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String,Object> handleUserNotExistException(UserNotExitException u){
        HashMap<String, Object> re = new HashMap<>();
        re.put("id", u.getId());
        re.put("message", u.getMessage());
        return re;
    }

}
